<?php

namespace App\Logic\Account;

use App\Component\LogListForm;
use App\Core\BaseCore;
use App\Core\BaseForm;
use App\Core\BaseListForm;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Exception\ValidationException;
use App\Core\Output;
use App\Enums\ValidateEnums;
use App\Logic\Account\Form\CancelRechargeForm;
use App\Logic\Account\Form\ConfirmRechargeForm;
use App\Logic\Account\Form\RechargeListForm;
use App\Logic\Account\Form\SubmitMemberRechargeForm;
use App\Logic\Account\Form\SubmitRechargeForm;
use App\Model\Account\RechargeModel;
use App\Util\ArrayHelper;
use App\Util\CommonHelper;
use EasySwoole\Component\Di;
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config as ESConfig;
use EasySwoole\Http\Request;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Utility\Random;
use EasySwoole\Validate\Validate;
use Throwable;

class RechargeService extends BaseCore
{

    use Singleton;

    /**
     * 字典数据
     * @param Request $request
     * @return Output
     * @throws Exception
     * @throws Throwable
     */
    public function getDict(Request $request) : Output
    {
        $result = [
            'recharge_state'   => ArrayHelper::keyArrayToList(RechargeModel::getRechargeStateDict()),
            'payment_type'    => ArrayHelper::keyArrayToList(RechargeModel::getPaymentTypeDict()),
            'payment_state'   => ArrayHelper::keyArrayToList(RechargeModel::getPaymentStateDict()),
        ];

        return $this->success($result);
    }

    /**
     * 充值
     * @param $data
     * @return Output
     * @throws DefaultException
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function create($data) : Output
    {
        $RechargeForm = new SubmitRechargeForm($data);
        $result = $RechargeForm->submit();
        return $result;
    }

    /**
     * 用户提交充值信息
     * @param $data
     * @return Output
     * @throws DefaultException
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function memberCreate($data) : Output
    {
        $RechargeForm = new SubmitMemberRechargeForm($data);
        $result = $RechargeForm->submit();
        return $result;
    }

    /**
     * 确定用户充值到账 confirm
     * @param $data
     * @return Output
     * @throws Throwable
     */
    public function confirm($data) : Output
    {
        $RechargeForm = new ConfirmRechargeForm($data);
        $result = $RechargeForm->submit();
        return $result;
    }

    /**
     * 取消用户充值
     * @param $data
     * @return Output
     * @throws Throwable
     */
    public function cancel($data) : Output
    {
        $RechargeForm = new CancelRechargeForm($data);
        $result = $RechargeForm->submit();
        return $result;
    }

    /**
     * 列表 getLists
     * @param Request $request
     * @return Output
     * @throws DefaultException
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\Redis\Exception\RedisException
     */
    public function getLists(Request $request){
        $params    = $request->getQueryParams();
        $cache     = RedisPool::defer();
        $cache_key = "rechargeCache:lists:" . json_encode($params);
        if ($cache_data = $cache->get($cache_key)) { //缓存存在
            $data = json_decode($cache_data, true);
        } else {
            $form = new RechargeListForm($params, $request);
            $data = $form->lists();
            $cache->set($cache_key, json_encode($data), BaseModel::CACHE_EXPIRE);
        }

        return $this->success($data);
    }


}
